﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Common;
using Entity;
using DAL;
using Models;
using System.Data.Linq;
using System.Linq.Expressions;
namespace BAL
{
   public  class User_Subject:BaseBAL<User_SubjectInfo,Models.User_Subject>
    {
       internal static User_SubjectInfo TransQuestion2QuestionInfo(Models.User_Subject usersubject)
       {
           User_SubjectInfo usersubjectinfo = new User_SubjectInfo();

           Utility.SetObjectValueWithTheSamePropertyName(usersubjectinfo, usersubject);

           return usersubjectinfo;
       } 

        //通过用户Id查询关联课程是否存在
        public static bool CheckUserGuanlianSubjectByUserId(int userId) 
        {
            using (var db = DatabaseHelper.GetDashboardData()) 
            {
                bool aa =db.User_Subjects.Where(w => w.UserId == userId).Count() > 0;
                return aa;
            }
        }
        //通过userid获取课程编号
        public static List<int> getOnlyOneSubjectIdByUserId(int userid)
        {
            List<int> list =new List<int>();
            using (var db = DatabaseHelper.GetDashboardData()) 
            {

                var query = from item in db.User_Subjects
                            where item.UserId == userid
                            select item.SubjectId;
                foreach (var item in query)
                {
                    list.Add(item);
                }
                return list;
               // return null;
            }
        }
        public static User_SubjectInfo Save(User_SubjectInfo InputInfo)
        {
            if (InputInfo.Id == 0)
            {
                return Add(InputInfo);
            }
            else
            {
                Edit(InputInfo);
                return InputInfo;
            }
        }
       //添加方法
       public static void  ganliansubjectanduseridInsert(User_SubjectInfo usersubjectinfo)
       {
           Save(usersubjectinfo);
       }
       //修改方法
       public static void ganliansubjectanduseridEdit(User_SubjectInfo usersubjectinfo) 
       {
           Save(usersubjectinfo);
       }

       /// <summary>
       /// 删除信息
       /// </summary>
       /// <param name="UserId"></param>
       /// <param name="SubjectId"></param>
       public static void DelInfo(int UserId, int SubjectId)
       {
           IList<Expression<Func<Models.User_Subject, bool>>> funcs = new List<Expression<Func<Models.User_Subject, bool>>>();
           if (UserId > 0)
           {
               funcs.Add(t => t.UserId == UserId);
           }
           if (SubjectId > 0)
           {
               funcs.Add(t => t.SubjectId == SubjectId);
           }
           using (var db = DatabaseHelper.GetDashboardData())
           {
               IList<int> ids = GetT2IQueryable(funcs, db).Select(t => t.Id).ToList();
               if (ids.Count > 0)
               {
                   Delete(ids);
               }
           }
       }

       public static IList<int> GetIds(int userid, int subjectid)
       {
           using(var db=DatabaseHelper.GetDashboardData())
           {
               if (userid > 0)
               {
                   return db.GetTable<Models.User_Subject>().Where(t => t.UserId == userid).Select(t => t.SubjectId).ToList();
               }
               else
               {
                   return db.GetTable<Models.User_Subject>().Where(t => t.SubjectId == subjectid).Select(t => t.UserId).ToList();
               }
           }
       }
    }
}
